package com.example.controller;

import com.example.entity.DocumentFolder;
import com.example.service.DocumentFolderService;
import com.example.service.KnowledgeSpaceService;
import com.example.common.Result;
import com.example.dto.CreateFolderRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/folders")
public class DocumentFolderController {

    @Autowired
    private DocumentFolderService documentFolderService;

    @Autowired
    private KnowledgeSpaceService knowledgeSpaceService;

    /**
     * 创建新文件夹
     */
    @PostMapping
    public Result createFolder(@RequestBody CreateFolderRequest request,
            @RequestParam String username) {
        try {
            // 检查用户是否有编辑权限 - 暂时禁用权限控制
            // if (!knowledgeSpaceService.hasEditPermission(request.getSpaceId(), username))
            // {
            // return Result.error("没有在该知识空间创建文件夹的权限");
            // }

            DocumentFolder folder = documentFolderService.createFolder(
                    request.getFolderName(),
                    request.getSpaceId(),
                    request.getParentFolderId(),
                    username);
            return Result.success(folder);
        } catch (Exception e) {
            return Result.error("创建文件夹失败: " + e.getMessage());
        }
    }

    /**
     * 根据文件夹ID获取文件夹详情
     */
    @GetMapping("/{folderId}")
    public Result getFolderById(@PathVariable Integer folderId, @RequestParam String username) {
        try {
            DocumentFolder folder = documentFolderService.getFolderById(folderId);
            if (folder == null) {
                return Result.error("文件夹不存在");
            }

            // 检查用户是否有查看权限 - 暂时禁用权限控制
            // if (!knowledgeSpaceService.hasViewPermission(folder.getSpaceId(), username))
            // {
            // return Result.error("没有访问该文件夹的权限");
            // }

            return Result.success(folder);
        } catch (Exception e) {
            return Result.error("获取文件夹失败: " + e.getMessage());
        }
    }

    /**
     * 根据空间ID获取文件夹树结构
     */
    @GetMapping("/space/{spaceId}/tree")
    public Result getFolderTree(@PathVariable Integer spaceId, @RequestParam String username) {
        try {
            // 检查用户是否有查看权限 - 暂时禁用权限控制
            // if (!knowledgeSpaceService.hasViewPermission(spaceId, username)) {
            // return Result.error("没有访问该知识空间的权限");
            // }

            List<DocumentFolder> folderTree = documentFolderService.buildFolderTree(spaceId);
            return Result.success(folderTree);
        } catch (Exception e) {
            return Result.error("获取文件夹树失败: " + e.getMessage());
        }
    }

    /**
     * 根据空间ID获取根目录文件夹列表
     */
    @GetMapping("/space/{spaceId}/root")
    public Result getRootFolders(@PathVariable Integer spaceId, @RequestParam String username) {
        try {
            // 检查用户是否有查看权限 - 暂时禁用权限控制
            // if (!knowledgeSpaceService.hasViewPermission(spaceId, username)) {
            // return Result.error("没有访问该知识空间的权限");
            // }

            List<DocumentFolder> folders = documentFolderService.getRootFoldersBySpaceId(spaceId);
            return Result.success(folders);
        } catch (Exception e) {
            return Result.error("获取根目录文件夹失败: " + e.getMessage());
        }
    }

    /**
     * 根据父文件夹ID获取子文件夹列表
     */
    @GetMapping("/{parentFolderId}/subfolders")
    public Result getSubFolders(@PathVariable Integer parentFolderId, @RequestParam String username) {
        try {
            DocumentFolder parentFolder = documentFolderService.getFolderById(parentFolderId);
            if (parentFolder == null) {
                return Result.error("父文件夹不存在");
            }

            // 检查用户是否有查看权限 - 暂时禁用权限控制
            // if (!knowledgeSpaceService.hasViewPermission(parentFolder.getSpaceId(),
            // username)) {
            // return Result.error("没有访问该文件夹的权限");
            // }

            List<DocumentFolder> subFolders = documentFolderService.getSubFoldersByParentId(parentFolderId);
            return Result.success(subFolders);
        } catch (Exception e) {
            return Result.error("获取子文件夹失败: " + e.getMessage());
        }
    }

    /**
     * 更新文件夹名称
     */
    @PutMapping("/{folderId}")
    public Result updateFolderName(@PathVariable Integer folderId,
            @RequestParam String newFolderName,
            @RequestParam String username) {
        try {
            documentFolderService.updateFolderName(folderId, newFolderName, username);
            return Result.success("文件夹名称更新成功");
        } catch (Exception e) {
            return Result.error("更新文件夹名称失败: " + e.getMessage());
        }
    }

    /**
     * 删除文件夹
     */
    @DeleteMapping("/{folderId}")
    public Result deleteFolder(@PathVariable Integer folderId, @RequestParam String username) {
        try {
            documentFolderService.deleteFolder(folderId, username);
            return Result.success("文件夹删除成功");
        } catch (Exception e) {
            return Result.error("删除文件夹失败: " + e.getMessage());
        }
    }

    /**
     * 获取文件夹的完整路径
     */
    @GetMapping("/{folderId}/path")
    public Result getFolderPath(@PathVariable Integer folderId, @RequestParam String username) {
        try {
            DocumentFolder folder = documentFolderService.getFolderById(folderId);
            if (folder == null) {
                return Result.error("文件夹不存在");
            }

            // 检查用户是否有查看权限 - 暂时禁用权限控制
            // if (!knowledgeSpaceService.hasViewPermission(folder.getSpaceId(), username))
            // {
            // return Result.error("没有访问该文件夹的权限");
            // }

            List<DocumentFolder> path = documentFolderService.getFolderPath(folderId);
            return Result.success(path);
        } catch (Exception e) {
            return Result.error("获取文件夹路径失败: " + e.getMessage());
        }
    }

    /**
     * 根据空间ID获取所有文件夹列表
     */
    @GetMapping("/space/{spaceId}")
    public Result getFoldersBySpace(@PathVariable Integer spaceId, @RequestParam String username) {
        try {
            // 检查用户是否有查看权限 - 暂时禁用权限控制
            // if (!knowledgeSpaceService.hasViewPermission(spaceId, username)) {
            // return Result.error("没有访问该知识空间的权限");
            // }

            List<DocumentFolder> folders = documentFolderService.getFoldersBySpaceId(spaceId);
            return Result.success(folders);
        } catch (Exception e) {
            return Result.error("获取文件夹列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据空间ID和创建者获取文件夹列表
     */
    @GetMapping("/space/{spaceId}/creator/{creatorUsername}")
    public Result getFoldersByCreator(@PathVariable Integer spaceId,
            @PathVariable String creatorUsername,
            @RequestParam String username) {
        try {
            // 检查用户是否有查看权限 - 暂时禁用权限控制
            // if (!knowledgeSpaceService.hasViewPermission(spaceId, username)) {
            // return Result.error("没有访问该知识空间的权限");
            // }

            List<DocumentFolder> folders = documentFolderService.getFoldersBySpaceAndCreator(spaceId, creatorUsername);
            return Result.success(folders);
        } catch (Exception e) {
            return Result.error("获取文件夹列表失败: " + e.getMessage());
        }
    }
}